<template>
    <div class="edit_container">
        <div class="btn_box">
            <el-button @click.stop="goBack">返回</el-button>
        </div>
        <Rehab ref="rehab" form-type="edit" :patient-info="patientInfo" :assess-info="assessInfo"
            :doctor-type="doctorType" title="初期康复评定记录" :signType="signType"/>
        <div class="sign_type_box">
            <SignTypeChoose fileName="初期康复评定记录" :signType="signType" @changeSignType="handleSignTypeChange"/>
        </div>
        <div class="hand_sign_box" v-if="signType == 2">
            <div>患者签名：</div>
            <HandSign stepOneTitle="打印评定记录" :imageList="signFileList" @doPrint="handlePrint" @updateFileList="handleFileListChange"/>
        </div>
        <div class="submit_btn_box">
            <el-button type="primary" :loading="submitLoading" @click.stop="submit">保存</el-button>
        </div>
        <div ref="printBox" class="print_box">
            <Rehab form-type="view" :patient-info="patientInfo" :assess-info="printAssessInfo"
            :doctor-type="doctorType" title="初期康复评定记录" />
        </div>
    </div>
</template>

<script>
import Rehab from '../../assessSys/components/rehab.vue'
import { recoveryDetail, recoveryRecord } from '@/api/treatment/treatment.js'
import { queryPatientInfo } from '@/api/residentPhysician/cureBilling.js'
import moment from 'moment'
import { mapGetters } from 'vuex'
import estimate from '@/mixins/estimate.js'
import { 
    getHemiplegiaContenByAssess, 
    getBruseContenByAssess, 
    getSittingContentByAssess, 
    getStandingContentByAssess, 
    getHandContentByAssess, 
    getDrinkingContentByAssess 
} from '../assessUtils'
import HandSign from "@/components/HandSign/index.vue"
import SignTypeChoose from "@/components/SignTypeChoose/index.vue"

export default {
    name: 'EarlyEdit',
    components: { Rehab, HandSign, SignTypeChoose },
    mixins: [estimate],
    data() {
        return {
            assessId: 0,
            type: 0,
            source: 1,
            patientId: 0,
            patientInfo: {},
            assessInfo: {},
            submitLoading: false,
            printAssessInfo: {},
            signType: 1,
            signFileList: []
        }
    },
    computed: {
        ...mapGetters([
            'user'
        ]),
        doctorType() {
            const sourceMap = {
                1: 'hospital',
                2: 'outpatient',
                3: 'therapists'
            }
            return sourceMap[this.source]
        }
    },
    mounted() {
        this.type = this.$route.query.type || 0
        this.source = this.$route.query.source
        this.assessId = this.$route.query.id
        this.patientId = this.$route.query.patientId
        this.getAssessInfo()
    },
    methods: {
        handleFileListChange(value) {
            console.log(value)
            this.signFileList = value
        },
        handleSignTypeChange(value) {
            this.signType = value
        },
        getAssessInfo(assessId) {
            // 执行获取治疗详情
            if(this.assessId == 0 && this.patientId == 0){
                this.$message.error('缺少医嘱ID或者患者ID')
                return
            }
            if(this.assessId){
                recoveryDetail({
                    type: this.type,
                    evaluateSource: this.source,
                    relationId: this.assessId,
                    estimateType: 1
                }).then(res => {
                    this.patientInfo = {
                        ...res.content,
                        genderName: res.content.gender === 0 ? '女' : '男'
                    }
                    if (res.content.evaluateTime) {
                        res.content.evaluateTime = moment(res.content.evaluateTime).format('YYYY-MM-DD HH:mm')
                    } else {
                        res.content.evaluateTime = '/'
                    }

                    if (!res.content.evaluateUser) {
                        res.content.evaluateUser = this.user.realname
                    }
                    this.assessInfo = {
                        ...this.assessInfo,
                        ...res.content
                    }
                })
            }else{
                queryPatientInfo({
                    page: 1,
                    size: 10,
                    hisPatientId: this.patientId
                }).then(response => {
                    let { code, content } = response;
                    let patientInfo = {}
                    if (code === 0 && content) {
                        patientInfo = content[0];
                        console.log(patientInfo)
                    } else {
                        patientInfo = {};
                    }
                    this.patientInfo = {
                        patientName: patientInfo.patientName,
                        genderName: patientInfo.gender === 0 ? '女' : '男',
                        age: patientInfo.age,
                        departmentName: patientInfo.departmentName,
                        admissionNum: patientInfo.admissionNum,
                        sickRegion: patientInfo.sickRegion,
                        bedNum: patientInfo.bedNum
                    }
                    let estimate4 = this.getEstimate(this.source, this.patientId, 4)
                    if(estimate4){
                        let localAssessInfo = this.getAssessInfoByEstimate(estimate4)
                        this.assessInfo = {
                            ...this.patientInfo,
                            ...localAssessInfo,
                            evaluateUser: this.user.realname,
                            evaluateTime: '/',
                            evaluateAddr: patientInfo.addr,
                        }
                    }else{
                        let hemiplegiaContent = ''
                        let estimate7 = this.getEstimate(this.source, this.patientId, 7)
                        if(estimate7){
                            let assessInfo7 = this.getAssessInfoByEstimate(estimate7);
                            hemiplegiaContent = getHemiplegiaContenByAssess(assessInfo7)
                        }
                        let bruseContent = ''
                        let sittingContent = ''
                        let standingContent = ''
                        let handContent = ''
                        let drinkingContent = ''
                        let estimate9 = this.getEstimate(this.source, this.patientId, 9)
                        if(estimate9){
                            let assessInfo9 = this.getAssessInfoByEstimate(estimate9)
                            bruseContent = getBruseContenByAssess(assessInfo9, 1)
                            sittingContent = getSittingContentByAssess(assessInfo9, 1)
                            standingContent = getStandingContentByAssess(assessInfo9, 1)
                            handContent = getHandContentByAssess(assessInfo9, 1)
                            drinkingContent = getDrinkingContentByAssess(assessInfo9, 1)
                        }
                        this.assessInfo = {
                            ...this.patientInfo,
                            evaluateUser: this.user.realname,
                            evaluateTime: '/',
                            evaluateAddr: patientInfo.addr,
                            admissionIllness: hemiplegiaContent,
                            ptQuestion: bruseContent + sittingContent + standingContent,
                            otQuestion: handContent,
                            stQuestion: drinkingContent
                        }
                    }
                    
                }).catch(error => {
                    this.$notify({
                        title: '提示',
                        message: '网络出差了,请稍后再试',
                        type: 'danger'
                    });
                })
            }
        },
        goBack() {
            this.$store.dispatch('tagsView/delView', this.$route)
            this.$router.back()
        },
        submit() {
            if(this.assessId){
                this.$refs.rehab.getFormData().then(data => {
                    this.submitLoading = true
                    recoveryRecord({
                        estimateType: 1,
                        relationId: this.assessId,
                        evaluateSource: this.source,
                        ...data,
                        patientSignType: this.signType,
                        patientSign: this.signType == 1 ? '1' : JSON.stringify(this.signFileList),
                        patientFingerprint: this.signType == 1 ? '1' : '[]',
                        patientSignTime: this.signType == 1 ? '1' : moment().format('YYYY-MM-DD HH:mm')
                    }).then(res => {
                        this.$message.success('成功')
                        this.$store.dispatch('tagsView/delView', this.$route)
                        this.$router.back()
                    }).catch(err => {
                        this.$message.error(err.msg)
                    }).finally(() => {
                        this.submitLoading = false
                    })
                })
            }else{
                this.$refs.rehab.saveLocalFormData(this.source, this.patientId, 1).then(() => {
                    this.$message.success('成功')
                    this.$store.dispatch('tagsView/delView', this.$route)
                    this.$router.back()
                })
            }
        },
        handlePrint() {
            this.$refs.rehab.getFormData().then(data => {
                this.printAssessInfo = {
                    ...data,
                    evaluateTime: moment().format('YYYY-MM-DD HH:mm')
                }
                this.$refs.printBox.style.display = "block";
                this.$nextTick(() => {
                    this.$print(this.$refs.printBox)
                    this.$refs.printBox.style.display = "none";
                })
            })
        }
    }
}
</script>

<style lang="scss" scoped>
@import url('./style.scss');
.sign_type_box, .hand_sign_box{
    width: 750px;
    margin: 0 auto;
}
.print_box{
    display: none;
}
</style>
